﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XGameFrame.Graphic;

namespace XGameFrame.Controls
{

    // A delegate for the Collision
    public delegate void CollisionWithControl(object sender, EventArgs e);

    // A delegate for Selection from controls
    public delegate void SelectedControls(object sender, EventArgs e);

    public abstract class Control:CoreGraphicElement
    {
        #region Field Region

        protected string text;
        protected object value;
        protected bool hasFocus;
        protected bool tabStop;
        protected SpriteFont spriteFont;
        protected Color color;
        protected string type;

        #endregion

        #region Event Region

        public event SelectedControls Selected;
        public event CollisionWithControl Collision;

        #endregion

        #region Property Region

        public string Text
        {
            get { return text; }
            set { text = value; }
        }
        public object Value
        {
            get { return value; }
            set { this.value = value; }
        }
        public bool HasFocus
        {
            get { return hasFocus; }
            set { hasFocus = value; }
        }
        public bool TabStop
        {
            get { return tabStop; }
            set { tabStop = value; }
        }
        public SpriteFont SpriteFont
        {
            get { return spriteFont; }
            set { spriteFont = value; }
        }
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }
        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        #endregion

        #region Constructor Region
        /// <summary>
        /// Constructor without Bounds,Size,SourceRectangle,DesRectangle apply
        /// the inherited class have to implement this
        /// </summary>
        /// <param name="pPosition"></param>
        public Control(Vector2 pPosition)
            :base(pPosition)
        {
            Color = Color.White;
            Enabled = true;
            Visible = true;
            SpriteFont = ControlManager.SpriteFont;
        }
        /// <summary>
        /// Constructor without Bounds,Size,SourceRectangle,DesRectangle apply
        /// the inherited class have to implement this
        /// </summary>
        /// <param name="pPosition"></param>
        /// <param name="pImage"></param>
        public Control(Texture2D pImage, Vector2 pPosition)
            :base(pImage,pPosition)
        {
            Color = Color.White;
            Enabled = true;
            Visible = true;
        }

        #endregion

        #region Abstract Methods

        
        public abstract void Draw(SpriteBatch spriteBatch);
        public abstract void HandleInput(PlayerIndex playerIndex);
        public abstract void OnHover(PlayerIndex playerIndex);

        #endregion

        #region Virtual Methods

        protected virtual void OnSelected(EventArgs e)
        {
            if (Selected != null)
            {
                Selected(this, e);
            }
        }

        public virtual void Update(GameTime gameTime)
        {
            //UpdateBoundPositions();
        }

        protected virtual Texture2D[,] LoadPics(Texture2D pSource, int pHorizontal, int pVertical)
        {

            Texture2D[,] anim = new Texture2D[pHorizontal, pVertical];
            Texture2D source = pSource;

            for (int x = 0; x < pHorizontal; x++)
            {
                for (int y = 0; y < pVertical; y++)
                {
                    //anim[x, y] = pSource.GetData<Texture2D>(pSource.GraphicsDevice, 0, 1);
                }
            }

            return anim;
        }

        protected virtual void OnCollision(EventArgs e)
        {
            if (Collision != null)
                Collision(this, e);
        }

        #endregion

        #region Methods
        public Vector2 getSizeForText(String pText)
        {
            return new Vector2(SpriteFont.MeasureString(pText).X, SpriteFont.MeasureString(pText).Y);
        }
        #endregion
    }
}
